package cn.poco.pepsi.util.xmlpared;

import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;

import android.R.integer;
import android.util.Xml;
import cn.poco.pepsi.bean.Area;
import cn.poco.pepsi.bean.Comment;
import cn.poco.pepsi.bean.HotSearchKey;

public class CommentXmlparse {
	//private final static String TAG = "DisAreaXmlparse";
	private static ArrayList<Comment> commentlist = null;
	private static  Comment commentBean = null;
	
	public static ArrayList<Comment> getXml(InputStream inSteam) throws Exception{
		XmlPullParser xmpparse = Xml.newPullParser();   //寰楀埌瑙ｉ噴鍣�
		xmpparse.setInput(inSteam, "UTF-8");           //璁剧疆瑙ｉ噴娴�
		int code = xmpparse.getEventType();            //寰楀埌鑺傜偣淇℃伅
		
		
		while(code!=XmlPullParser.END_DOCUMENT){
			switch (code) {
			case XmlPullParser.START_DOCUMENT:
				commentlist = new ArrayList<Comment>();
				break;
			case XmlPullParser.START_TAG:
				if("grade".equals(xmpparse.getName())){
					commentBean = new Comment();
				}
				if(commentBean!=null){
					if("user-name".equals(xmpparse.getName())){
						String keyw = xmpparse.nextText();
						commentBean.setName(keyw);
					}if("user-id".equals(xmpparse.getName())){
						String keyw = xmpparse.nextText();
						commentBean.setId(Integer.parseInt(keyw));
					}if("food-level".equals(xmpparse.getName())){
						String keyw = xmpparse.nextText();
						commentBean.setLevel(keyw);
					}if("produce".equals(xmpparse.getName())){
						String keyw = xmpparse.nextText();
						commentBean.setProduce(Integer.parseInt(keyw));
					}if("service".equals(xmpparse.getName())){
						String keyw = xmpparse.nextText();
						commentBean.setService(Integer.parseInt(keyw));
					}if("average".equals(xmpparse.getName())){
						String keyw = xmpparse.nextText();
						commentBean.setAverage(keyw);
					}if("imgurl".equals(xmpparse.getName())){
						String keyw = xmpparse.nextText();
						commentBean.setImgurl(keyw);
					}if("grade-content".equals(xmpparse.getName())){
						String keyw = xmpparse.nextText();
						commentBean.setContent(keyw);
					}
					
					
				}
				break;
			case XmlPullParser.END_TAG:
				if("grade".equals(xmpparse.getName())){
					commentlist.add(commentBean);
					commentBean = null;
				}
				break;
			}	
			
			try {
				code = xmpparse.next();
				System.out.println("code = xmpparse.next();正常");
			} catch (Exception e) {
				System.out.println("有异常");
				code=XmlPullParser.END_DOCUMENT;
			}
			
		}
	    inSteam.close();
		System.out.println("xmlparse---->keys.size"+commentlist.size());
		
		if(commentlist.size()==0){
			
			ArrayList<Comment> testlist=new ArrayList<Comment>();
			Comment testObj=new Comment();
			testObj.setName("中国人民共和共");
			testlist.add(testObj);
			System.out.println("return testlist;");
			return testlist;
			
		}else{
			System.out.println("XML——————》return  keys;");
			return  commentlist;
		}
		
		
		
		
	}
}
